package com.ffggffgfv.backend.util;

import com.ffggffgfv.backend.common.ResultCode;

/**
 * 统一API响应结果封装
 */
public class Result {

    private int code;
    private String msg;
    private Object data;
    private String comments;

    protected Result() {
    }

    protected Result(int code, String msg, Object data, String comments) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.comments = comments;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static Result success(Object data) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data, null);
    }

    /**
     * 成功返回结果
     *
     * @param data     获取的数据
     * @param comments 备注
     */
    public static Result success(Object data, String comments) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data, comments);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static Result failed(ResultCode errorCode) {
        return new Result(errorCode.getCode(), errorCode.getMessage(), null, null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static Result failed(ResultCode errorCode, String message) {
        return new Result(errorCode.getCode(), message, null, null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static Result failed(String message) {
        return new Result(ResultCode.FAILED.getCode(), message, null, null);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static Result validateFailed(String message) {
        return new Result(ResultCode.VALIDATE_FAILED.getCode(), message, null, null);
    }

    /**
     * 未登录返回结果
     */
    public static Result unauthorized() {
        return new Result(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), null, null);
    }

    /**
     * 未登录返回结果
     *
     * @param message 提示信息
     */
    public static Result unauthorized(String message) {
        return new Result(ResultCode.UNAUTHORIZED.getCode(), message, null, null);
    }

    /**
     * 未授权返回结果
     */
    public static Result forbidden() {
        return new Result(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), null, null);
    }

    /**
     * 未授权返回结果
     *
     * @param message 提示信息
     */
    public static Result forbidden(String message) {
        return new Result(ResultCode.FORBIDDEN.getCode(), message, null, null);
    }

    /**
     * 资源冲突返回结果
     */
    public static Result conflict(String message) {
        return new Result(ResultCode.CONFLICT.getCode(), message, null, null);
    }

    /**
     * 服务器内部错误返回结果
     */
    public static Result internalError(String message) {
        return new Result(ResultCode.INTERNAL_ERROR.getCode(), message, null, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }
}